﻿Imports System.IO
Imports System.Net.Sockets

Public Class ConnectedClient

    Private client As TcpClient
    Private stream As NetworkStream
    Private reader As StreamReader
    Private writer As StreamWriter

    Public Sub New(ByVal client As TcpClient)
        Me.client = client
        stream = client.GetStream()
        reader = New StreamReader(stream)
        writer = New StreamWriter(stream)
    End Sub

    Public Function ReadData() As String
        Dim data As String = ""

        Try
            data = reader.ReadLine()
        Catch ex As Exception
            Console.WriteLine(">> Failed to read data - Client disconnected")
        End Try

        Return data
    End Function

    Public Function isConnected() As Boolean
        Return client.Connected
    End Function

    Public Sub SendData(ByVal data As String)
        Try
            writer.WriteLine(data)
            writer.Flush()
        Catch ex As Exception
            Console.WriteLine(">> Failed to write data - Client disconnected")
        End Try
    End Sub

    Public Sub ShutDownClient()
        reader.Close()
        writer.Close()
        stream.Close()
        client.Close()
    End Sub
End Class
